from .common import *
'''
    视图创建数值集中处理模块
'''

class ViewTypeConfigError(Exception): ...

def read_type_json()->Dict[str, Any]:
    with open(VIEW_TYPE_JSON_PATH, encoding='utf-8') as f:
        data = json.load(f)
    return data

def read_data_json()->Dict[str, Any]:
    with open(VIEW_DATA_JSON_PATH, encoding='utf-8') as f:
        data = json.load(f)
    return data

def read_data_args_json()->Dict[str, Any]:
    with open(VIEW_DATA_ARGS_JSON_PATH, encoding='utf-8') as f:
        data = json.load(f)
    return data

'''
    常用操作
'''

def get_all_views_type_name()->List[str]:
    """获取所有的视图类型（包括函数和类）"""
    view_type_data = read_type_json()
    return [_.get('name') for _ in view_type_data.values()]

def get_listdata_by_view_type_name(view_type_name: str, arg: str='tips')->List[str]:
    """通过视图类型的名称获取 列表型数值
    
        arg可选值：tips、attrs、override
    """
    for _ in read_type_json().values():
        if view_type_name == _.get('name'):
            list_obj = _.get(arg)
            return list_obj if list_obj is not None else []
    return []

def get_str_by_view_type_name(view_type_name: str, arg: str='type')->List[str]:
    """通过视图类型的名称获取 列表型数值
    
        arg可选值：tips、extends、override
    """
    for _ in read_type_json().values():
        if view_type_name == _.get('name'):
            str_obj = _.get(arg)
            return str_obj if str_obj is not None else ""
    return ""

def get_type_name_by_name(view_type_name: str)->str:
    """通过 值name 获取 键type_func"""
    for k, v in read_type_json().items():
        if v.get('name') == view_type_name:
            return k
    raise ViewTypeConfigError("view_type.json 文件已损坏！")

def get_view_data_by_name(view_type_name: str)->str:
    """通过view_type.json的name值，获取view_data.json中对应的template代码"""
    type_name = get_type_name_by_name(view_type_name)
    code = "\n".join(read_data_json()[type_name]["template"])
    return code

def get_replace_args_by_replace_name(replace_name: str)->List[str]:
    """view_data_args.json 值获取事件"""
    datas = read_data_args_json().get(replace_name)
    if datas is None:
        return []
    else:
        return datas
